<script>
	import Vue from 'vue';
	import config from './utils/config.js';
	export default {
		globalData: {
			CustomBar: 40,
			userInfo: {},
			hasUserInfo: false, //未授权
			imgUrl: 'https://6365-ceart-bh8ws-1300171498.tcb.qcloud.la/', //云存储路径
			version: "V1.0.0",
			blogName: "Ceart博客",
			domain: "http://120.79.230.50",
			logo: 'static/qrcode.jpg',
			admire: 'static/admire.png', //赞赏图
			user_photo: 'static/user.png',
			QRcode: 'static/QRcode.jpg' ,//二维码
			color:'green',
			isExamine:true,//审核
			isReply:false,//回复
			isShowDonate:false,//开启赞赏
			admin_id:'oQUZ45Ixc6vBdKsWxgQkw94QSqs8',
		},
		onLaunch:function() {
			if (!wx.cloud) {
				console.error('请使用 2.2.3 或以上的基础库以使用云能力')
			} else {
				wx.cloud.init({
					traceUser: true,
					env: 'ceart-bh8ws'
				})
			}
			
			let api=require('./utils/api.js');
			
			var _this = this;
			//全局配置
			new Promise(resolve => {
					resolve(api.getGlobal());
			}).then(result => {
				if(result.result.data.length>0){
					let conf = result.result.data[0];
					this.globalData.imgUrl=conf.imgUrl;
					this.globalData.version=conf.version;
					this.globalData.blogName=conf.blogName;
					this.globalData.domain=conf.domain;
					this.globalData.logo=conf.logo;
					this.globalData.admire=conf.admire;
					this.globalData.user_photo=conf.user_photo;
					this.globalData.QRcode=conf.QRcode;
					this.globalData.isExamine=conf.isExamine;
					this.globalData.isReply=conf.isReply;
					this.globalData.isShowDonate=conf.isShowDonate;
					this.globalData.admin_id=conf.admin_id;
				}
			})

			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN || MP-QQ
					Vue.prototype.StatusBar = e.statusBarHeight;
					let capsule = wx.getMenuButtonBoundingClientRect();
					if (capsule) {
						Vue.prototype.Custom = capsule;
						// Vue.prototype.capsuleSafe = uni.upx2px(750) - capsule.left + uni.upx2px(750) - capsule.right;
						Vue.prototype.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					}
					// #endif		


					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
					_this.globalData.CustomBar = Vue.prototype.CustomBar;
				}
			})

			Vue.prototype.ColorList = [{
					title: '嫣红',
					name: 'red',
					color: '#e54d42'
				},
				{
					title: '桔橙',
					name: 'orange',
					color: '#f37b1d'
				},
				{
					title: '明黄',
					name: 'yellow',
					color: '#fbbd08'
				},
				{
					title: '橄榄',
					name: 'olive',
					color: '#8dc63f'
				},
				{
					title: '森绿',
					name: 'green',
					color: '#39b54a'
				},
				{
					title: '天青',
					name: 'cyan',
					color: '#1cbbb4'
				},
				{
					title: '海蓝',
					name: 'blue',
					color: '#0081ff'
				},
				{
					title: '姹紫',
					name: 'purple',
					color: '#6739b6'
				},
				{
					title: '木槿',
					name: 'mauve',
					color: '#9c26b0'
				},
				{
					title: '桃粉',
					name: 'pink',
					color: '#e03997'
				},
				{
					title: '棕褐',
					name: 'brown',
					color: '#a5673f'
				},
				{
					title: '玄灰',
					name: 'grey',
					color: '#8799a3'
				},
				{
					title: '草灰',
					name: 'gray',
					color: '#aaaaaa'
				},
				{
					title: '墨黑',
					name: 'black',
					color: '#333333'
				},
				{
					title: '雅白',
					name: 'white',
					color: '#ffffff'
				},
			]
			Vue.prototype.updateUserInfo = function (open='') {
				let _this = this;
				wx.getSetting({
					success: (res) => {
						if (res.authSetting['scope.userInfo'] == undefined || res.authSetting['scope.userInfo'] == false) {
							console.log('用户取消授权用户信息')
							getApp().globalData.userInfo = {};
							getApp().globalData.hasUserInfo = false;
							_this.userInfo =  {};
							_this.hasUserInfo=false;
							if(open=='read'){
								_this.Operate={
									isbrowse: false, //是否浏览过
									iscolle: false, //是否收藏过
									iscomme: false, //是否评论
									isliek: false, //是否点赞
								}
							}
						} else {
							new Promise(resolve => {
									resolve(api.getUserInfo());
							}).then(result => {
								if(result.result.data.length>0){
									// 已经授权，可以直接调用 getUserInfo 获取头像昵称
									_this.userInfo = result.result.data[0];
									_this.hasUserInfo = true;
									getApp().globalData.userInfo = result.result.data[0];
									getApp().globalData.hasUserInfo = true;
				
									if(open=='read'){
										_this.myOperate();
									}
								}		
							})
						}
					}
				})
			};
		},
		onShow: function() {
			// console.log('App Show')		
			const updateManager=uni.getUpdateManager();    // 获取更新管理器对象
			updateManager.onCheckForUpdate(function(res){
				if(res.hasUpdate){
					updateManager.onUpdateReady(function(){
						uni.showModal({
							title:'更新提示',
							content:'新版本已经准备好，点击体验最新功能~',
							showCancel:false,
							success:res=>{
								if(res.confirm){
										// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
									updateManager.applyUpdate();
								}
							}
						})
					})
					updateManager.onUpdateFailed(function(){
						uni.showModal({
							title:'已经有新版本了哟~',
							content:'检查到有新版本，但是下载失败，请检查网络设置',
							showCancel:false
						})
					})
				}
			})
		},
		onHide: function() {
			// console.log('App Hide')
		}
	}
</script>

<style>
	@import "colorui/main.css";
	@import "colorui/icon.css";

	.noData {
		margin: 90rpx 0;
	}

	.noImg {
		margin: 0 auto;
		width: 270rpx;
		height: 270rpx;
		display: block;
	}

	.noTitle {
		display: block;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
	}
</style>
